﻿using Basic.FlowModel.AuditLogItem;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAuditItemLogDAL : BasicDAL<DBFlowAuditItemLog, long>, IFlowAuditItemLogDAL
    {
        public FlowAuditItemLogDAL ( IRepository<DBFlowAuditItemLog> basicDAL ) : base(basicDAL, "audit.log.item")
        {
        }
        public long[] GetNodeAuditEmpId ( long logId, long rootNodeId, long[] nodeId )
        {
            var tables = nodeId.ConvertAll(c =>
            {
                return this._BasicDAL.Queryable.Where(a => a.LogId == logId && a.NodeId == c && a.RootNodeId == rootNodeId && a.Status == FlowAuditItemStatus.审核通过).OrderByDescending(a => a.EndTime).Select(a => new
                {
                    a.BatchNo
                }).Take(1);
            });
            long[] batchNo = this._BasicDAL.Provider.UnionAll(tables).Select(a => a.BatchNo).ToArray();
            if ( batchNo.IsNull() )
            {
                return Array.Empty<long>();
            }
            return this._BasicDAL.Gets(a => batchNo.Contains(a.BatchNo) && a.Status == FlowAuditItemStatus.审核通过, a => a.EmpId).Distinct();
        }

        public Result[] GetItemLog<Result> ( long logId, long nodeId ) where Result : class, new()
        {
            long backNo = this._BasicDAL.Get(a => a.LogId == logId && a.NodeId == nodeId && a.PrtBatchNo == null, a => a.BatchNo, "CreateTime desc");
            if ( backNo == 0 )
            {
                return null;
            }
            return this._BasicDAL.Gets<Result>(a => a.BatchNo == backNo);
        }
        public AuditItemLog GetAuditLog ( long itemId )
        {
            return this._BasicDAL.JoinGet<DBFlowAuditLog, AuditItemLog>(( a, b ) => a.LogId == b.Id && a.Id == itemId, ( a, b ) => new AuditItemLog
            {
                PrevBackNo = a.PrevBackNo,
                PrevNodeId = a.PrevNodeId,
                AuditOpinion = a.AuditOpinion,
                FlowId = b.FlowId,
                BackNodeId = a.BackNodeId,
                DocTitle = b.DocTitle,
                BatchNo = a.BatchNo,
                EmpByDeptId = a.EmpByDeptId,
                BeginDeptId = b.CreateByDeptId,
                BeginCompanyId = b.CompanyId,
                CompanyId = a.CompanyId,
                BeginEmpId = b.CreateBy,
                RootBatchNo = a.RootBatchNo,
                NextDeptId = a.NextDeptId,
                BeginUnitId = b.CreateByUnitId,
                EmpByUnitId = a.EmpByUnitId,
                EmpId = a.EmpId,
                RootNodeId = a.RootNodeId,
                Id = a.Id,
                LinkPk = b.LinkPk,
                LinkType = b.LinkType,
                LogId = a.LogId,
                PrtBatchNo = a.PrtBatchNo,
                NodeId = a.NodeId,
                NodeType = a.NodeType,
                NodeLvl = a.NodeLvl,
                Status = a.Status,
                LogStatus = b.Status,
                LogAuditStatus = b.AuditStatus,
                AuditState = a.AuditState,
                MarkState = a.MarkState,
                OptItemId = a.OptItemId
            });
        }
    }
}
